﻿//
// TravModel public API for clipboard operations
//

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Diagnostics;

using FileTreeSpace;


namespace TravModelSpace
{
    public partial class TravModel
    {
        // Source: files/dirs (detail), Target: files/dirs
        public bool CopyFiles (string[] sourcePathNames, string targetPathName)
        {
            foreach (string sourceName in sourcePathNames)
            {
                bool isError = CopyFile (sourceName, targetPathName);
                if (isError)
                    return isError;
            }

            return false;
        }


        public bool CopyFile (string sourceName, string targetDirName)
        {
            string failure = null;
            try
            {
                DirectoryInfo sourceInfo = new DirectoryInfo (sourceName);

                if ((sourceInfo.Attributes & FileAttributes.Directory) != 0)
                {
                    Debug.WriteLine ("Copy newDir '" + targetDirName + '\\' + sourceInfo.Name + "'");

                    if (targetDirName.Length >= sourceName.Length)
                        if (targetDirName.Substring (0, sourceName.Length) == sourceName)
                            throw new IOException ("The destination folder is a subfolder of the source folder.");

                    Directory.CreateDirectory (targetDirName + '\\' + sourceInfo.Name);

                    FileSystemInfo[] fsInfos = sourceInfo.GetFileSystemInfos ();
                    foreach (FileSystemInfo fsInfo in fsInfos)
                    {
                        bool isError = CopyFile (fsInfo.FullName, targetDirName + '\\' + sourceInfo.Name);
                        if (isError)
                            return isError;
                    }
                }
                else
                {
                    // Copy file!
                    Debug.WriteLine ("Copy file '" + sourceName + "' to '" + targetDirName + '\\' + sourceInfo.Name + "'");
                    File.Copy (sourceName, targetDirName + '\\' + sourceInfo.Name, false);
                }
            }
            catch (IOException ex)
            {
                failure = ex.Message;
            }
            catch (UnauthorizedAccessException ex)
            {
                failure = ex.Message;
            }

            if (failure != null)
                Presenter.ShowMessage (failure, "Copy Failed");

            return failure != null;
        }


        // Target: directory, Source: files or directory
        public bool TransferFiles (string[] sourcePathNames, string targetDirPathName)
        {
            string failure = null;

            foreach (string sourceName in sourcePathNames)
            {
                try
                {
                    FileInfo file = new FileInfo (sourceName);
                    FileAttributes sourceAttribs = file.Attributes;
                    Debug.WriteLine ("Xfering file [" + sourceName + "] to [" + targetDirPathName + "]");

                    int i = sourceName.LastIndexOf ('\\');

                    if ((sourceAttribs & FileAttributes.Directory) == FileAttributes.Directory)
                    {
                        // Move directory!
                        DirectoryInfo sourceDirInfo = new DirectoryInfo (sourceName);
                        sourceDirInfo.MoveTo (targetDirPathName + sourceName.Substring (i));
                    }
                    else
                        // Copy file!
                        file.MoveTo (targetDirPathName + sourceName.Substring (i));

                }
                catch (IOException ex)
                {
                    failure = ex.Message;
                }
                catch (UnauthorizedAccessException ex)
                {
                    failure = ex.Message;
                }
            }

            if (failure != null)
                Presenter.ShowMessage (failure, "Move Failed");

            return failure != null;
        }
    }
}
